package com.lagou.edu.controller;

import com.lagou.edu.pojo.AuthCode;
import com.lagou.edu.pojo.Token;
import com.lagou.edu.service.ValicateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * @author lyj
 * @Title: UserController
 * @ProjectName homework-parent
 * @Description: TODO
 * @date 2021/1/2 20:12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ValicateService valicateService;
    /**
     * 登录
     * @param email
     * @param password
     * @return
     */
    @GetMapping("/login/{email}/{password}")
    public boolean login(HttpServletRequest request,HttpServletResponse response, @PathVariable("email")String email, @PathVariable("password") String password){
        Token token = valicateService.getTokenByEmail(email);
        if(token==null)return false;
        //查看是否有token
        boolean flag=false;
        Cookie[] cookies = request.getCookies();
        if(cookies!=null && cookies.length>0) {
            for (Cookie cookie : cookies) {
                if ("access_token".equals(cookie.getName())) {
                    if (!token.getToken().equals(cookie.getValue())) {
                        return false;
                    }
                    flag = true; //说明有token
                    break;
                }
            }
        }
                 //没token则添加token到cookie
                 if(!flag){

                     String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
                     Token ack=new Token();
                     ack.setEmail(email);
                     ack.setToken(uuid);
                     valicateService.saveToken(ack);

                     Cookie cookie = new Cookie("access_token", uuid);
                     cookie.setHttpOnly(true);
                     cookie.setPath("/");
                     cookie.setDomain("www.test.com");
                     response.addCookie(cookie);
                 }

        System.out.println("-----------------登录成功！！");


        return true;

    }

    /**
     * 注册
     * @param email
     * @param password
     * @param code
     * @return
     */
    @GetMapping("/register/{email}/{password}/{code}")
    public String register(HttpServletResponse response, @PathVariable("email")String email,
                           @PathVariable("password") String password,
                           @PathVariable("code") String code) throws IOException {



        AuthCode authCode = valicateService.getAuthCodeByEmail(email);
        if (authCode!=null && authCode.getCode().equals(code)) {
            String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
            Token token=new Token();
            token.setEmail(email);
            token.setToken(uuid);
            valicateService.saveToken(token);
            System.out.println("-----------------注册成功！！");

            Cookie cookie = new Cookie("access_token", uuid);
            cookie.setHttpOnly(true);
            cookie.setPath("/");
            cookie.setDomain("www.test.com");
            response.addCookie(cookie);

          //  response.sendRedirect("http://www.test.com/static/index.html");
            return "true";
        }
        return "验证码错误";


    }

    /**
     * 验证邮箱是否登录
     * @param email
     * @return
     */
    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable("email")String email){

        return true;

    }

    /**
     * 根据token查询邮箱
     * @param token
     * @return
     */
    @GetMapping("/info/{token}")
    public String info(@PathVariable("token")String token){

        return "";

    }



}
